package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class RadixSort {

	public static void main(String[] args) {
		int arr[] = { 53, 3, 542, 748, 14, 214};
		
		System.out.println(Arrays.toString(arr));
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("开始时间=" + date1Str);
		
		radixSort(arr);
		
		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("结束时间=" + date2Str);
		
	
		
	}

	
	public static void radixSort(int[] arr) {
		
		
		int max = arr[0]; 
		for(int i = 1; i < arr.length; i++) {
			if (arr[i] > max) {
				max = arr[i];
			}
		}
		int maxLength = (max + "").length();
		int[][] bucket = new int[10][arr.length];
		int[] bucketElementCounts = new int[10];
		
		for(int i = 0 , n = 1; i < maxLength; i++, n *= 10) {
			
			for(int j = 0; j < arr.length; j++) {
			
				int digitOfElement = arr[j] / n % 10;
			
				bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
				bucketElementCounts[digitOfElement]++;
			}
			
			int index = 0;
		
			for(int k = 0; k < bucketElementCounts.length; k++) {
			
				if(bucketElementCounts[k] != 0) {
					
					for(int l = 0; l < bucketElementCounts[k]; l++) {
					
						arr[index++] = bucket[k][l];
					}
				}
				
				bucketElementCounts[k] = 0;
				
			}
			System.out.println(i);
			System.out.println(n);
			System.out.println(Arrays.toString(arr));
			
		}
		
		
		
	}
}
